/*
 * Component.h
 *
 *  Created on: 28 Oct 2012
 *      Author: Guy
 */

#ifndef COMPONENT_H_
#define COMPONENT_H_

#include <SDL/SDL.h>
#include <list>
#include "types/Vector2D.h"
#include "types/Collision.h"
#include "core/Object.h"
#include "core/Time.h"

namespace SolidityEngine {

class GameObject;
class Component;
typedef std::unique_ptr<Component> Component_ptr;

template <class T>
class Parameter {
    protected:
        std::string _name;
        T _value;
    public:
        Parameter(const std::string& name, const T& value):
            _name(name),
            _value(value)
        {};
        T value() const {return _value;};
        T& value() {return _value;};
};

class Component : public Object {

protected:
	GameObject* _gameobject; //weak ptr to GameObject this component is attached to
	bool _enabled;
    Uint32 _time;
    Uint32 _interval;
    //std::list<Parameter> _parameters;

public:
    Component(const std::string& name);
    Component();
    virtual ~Component();

	friend class GameObject;
	const GameObject* GetGameObject() const{return _gameobject;}; //get a readonly handle to the gameobject
    GameObject* GetGameObject() {return _gameobject;}; //get a readonly handle to the gameobject

    bool Enabled() const {return _enabled;};
	bool& Enabled() {return _enabled;};

    void OnUpdate()
    {
        if (_enabled)
        {
            _OnUpdate();
            if (Time::CurrentTime()>_time+_interval)
            {
                _OnInterval();
                _time=Time::CurrentTime();
            }
        }
    }; // called when the owning gameObject receives and OnTick();
	void OnStart(){if (_enabled) _OnStart();};
	void OnDestroy(){if (_enabled) _OnDestroy();};// called when the owning gameObject is destroyed
    void OnCollision(const Collision_ptr& col){if (_enabled) _OnCollision(col);};

    //allow an update interval to be set
    void SetInterval(Uint32 time){_interval=time;};

private:
    virtual void _OnInterval(){};
    virtual void _OnUpdate(){}; // called when the owning gameObject receives and OnTick();
	virtual void _OnStart(){};
	virtual void _OnDestroy(){};// called when the owning gameObject is destroyed
    virtual void _OnCollision(const Collision_ptr& col){};
    virtual Component* _Clone() = 0;
    std::string _Serialize(){
        return std::string("Serialization not implemented for: ")+std::string(typeid(*this).name());
    };
};
};
#endif /* COMPONENT_H_ */
